/**
 * Created by Tuffy on 16/6/23.
 */
'use strict';

var salesCtrl = angular.module('sales.controllers', ['ui.router']);

salesCtrl.controller('SalesIndexCtrl', [function () {
     // alert("1");
}])
.controller('Demo1Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
    // alert("Demo1Ctrl");
}])

.controller('Demo2Ctrl', ['$rootScope', '$scope', '$http', '$location', function ($rootScope, $scope, $http, $location) {
	//配置
	$scope.count = 1; //数据总数
    $scope.p_pernum = 10; //每页显示条数
    //变量
    $scope.p_current = 1;  //当前页数
    $scope.p_all_page = 0;  //总页数
    $scope.pages = [];
    //初始化第一页
    getPage($scope.p_current,$scope.p_pernum,function(){
       //alert("我是第一次加载");
    });
    function getPage(page,size,callback){
     	// "xxx.html?status=0&page="+page+"&size="+size
		$http.get("json/demo2.json").success(function(res){

     	var thisData= res.data.concat().splice(page*10-10, 10);//当前页数*每页显示条数-每页显示条数,每页显示条数
       /*     if(res&&res.status==1){ */
      			$scope.count=res.count;
                $scope.names=thisData;
                $scope.p_current = page;
                $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);
                reloadPno();//在这调用的   也就是是说在第一次的时候 调用并加载 //看我鼠标锁选中的
                callback();
           /* }else{
                alert("加载失败");
            }  */
        });
    }
    //单选按钮选中
    $scope.select= function(id){
        //alert(id);
    }
    //首页
    $scope.p_index = function(){
        $scope.load_page(1);
    }
    //尾页
    $scope.p_last = function(){
        $scope.load_page($scope.p_all_page);
    }
    //加载某一页
    $scope.load_page = function(page){
 		getPage(page,$scope.p_pernum,function(){});
    };
    //跳转到指定页
    $scope.goBtn =function(){
    	var idx=$("#on").val();
    	getPage(idx,$scope.p_pernum,function(){});
    }
    //初始化页码
    var reloadPno = function(){
    	$scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,8);
    };

	var calculateIndexes = function (current, length, displayLength) {
	    var indexes = [];
	    var start = Math.round(current - displayLength / 2);
	    var end = Math.round(current + displayLength / 2);
	    if (start <= 1) {
	        start = 1;
	        end = start + displayLength - 1;
	       if (end >= length - 1) {
	           end = length - 1;
	        }
	    }
	    if (end >= length - 1) {
	       end = length;
	        start = end - displayLength + 1;
	       if (start <= 1) {
	           start = 1;
	        }
	    }
	    for (var i = start; i <= end; i++) {
	        indexes.push(i);
	    }
	    return indexes;
	}

	//全选
	$scope.selectAll=false;
    var isAllSelect=true;
    $scope.all= function (m) {
    	for(var i=0;i<$scope.names.length;i++){
          	if(m===true){
              	$scope.names[i].state=true;
           	alert("全选");
          	}else {
              	$scope.names[i].state=false;
           	alert("取消全选");
          	}
        }

    };
	//单选
    $scope.select=function(n){
    	if(this.x.state==true){
    		this.x.state=false;
 		alert("取消选中");
    	}else{
    		this.x.state=true;
 		alert("单选");
    	}
    }

	//搜索
  	$scope.mySea = true;//默认隐藏
    $scope.toggle = function() {
    	$scope.mySea = !$scope.mySea;
    }
    //删除
    $scope.delBtn =function(){
    	var recidList = $("input[name='id_name']:checked");
    	var recidVal = recidList.val();
    	if(!recidVal){
    		layer.msg('请选中删除项');
    	}else{
    		if(recidList.length>1){
				for(var i=0;i<recidList.length;i++){
					var checkVal = $(recidList[i]).val();
					var checkId = checkVal.split("-");
					layer.confirm('确定全部删除？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
					  layer.msg('已全部删除', {icon: 1});
					});
				}
			}else{
				for(var i=0;i<recidList.length;i++){
					var checkVal = $(recidList[i]).val();
					var checkId = checkVal.split("-");
					layer.confirm('确定删除？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
					  layer.msg('已删除', {icon: 1});
					});
				}
			}
    	}
	}
    //修改
    $scope.capBtn =function(n){
    	var recidList = $("input[name='id_name']:checked");
    	var recidVal = recidList.val();
    	if(!recidVal){
    		layer.msg('请选中修改项');
    	}else{
    		if(recidList.length>1){
				layer.msg('请选中单个修改项');
			}else{
				for(var i=0;i<recidList.length;i++){
					var checkVal = $(recidList[i]).val();
					var checkId = checkVal.split("-");
					layer.open({
					  type: 2,
					  title: '修改',
					  shadeClose: true,
					  shade: 0.4,
					  area: ['690px', '400px'],
					  content: 'capTab.html', //iframe的url
					  btnAlign: 'c', //按钮居中
					  btn:["确定","取消"],
					  yes:function(){
						layer.msg('确定');
					  }
					});
				}
			}
		}
	}
	//刷新
    $scope.refreshBtn=function(){
    	location.reload();
    }
	//添加
    $scope.addBtn =function(){
    	layer.open({
		  type: 2,
		  title: '添加',
		  shadeClose: true,
		  shade: 0.4,
		  area: ['690px', '400px'],
		  content: 'addTab.jsp', //iframe的url
		  btn:["确定","取消"],
		  btnAlign: 'c' //按钮居中
		});
    }
    //重置
    $scope.resetBtn=function(){
    	$("#showContainer input").val("");
    }
//	//起运地
//  $scope.starts = ["浙江省", "江西省", "福建省"];
//  //审批
//  $scope.selects = ["待审批", "已同意", "已拒绝"];
	//查看
	$scope.info =function(){
	  //-------------第一种方式------------//
	  var value = this.x.a;
	  // console.log(this.x)
	  // console.log(this.$parent.names)
	  //-------------第二种方式------------//
	  // 获取父级元素
   var $parentEle = this.$parent;
   // 获取表格中所有数据的数组对象
   var names = $parentEle.names;

   // 获取当前索引值，索引是从0开始的
   var currentIndex = this.$index;
   // 获取当前索引对应的ID值
   var value = names[currentIndex].a;

    }
}])
.controller('Demo3Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
//  alert("Demo2Ctrl");
}])
.controller('Demo4Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
	
}])
.controller('Demo5Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
	
}])
.controller('Demo6Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
	$scope.user = 'John Doe';
    $scope.email = 'john.doe@gmail.com';
}])
.controller('Demo7Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
}])
.controller('Demo8Ctrl', ['$rootScope', '$scope', function ($rootScope, $scope) {
	$scope.pen={
		money:1.5,
		number:1,
		trans:8
	}
	$scope.proPrice = function(){
		return $scope.pen.money*$scope.pen.number;
	} 
	$scope.$watch($scope.proPrice,function(newVal,oldVal){
		$scope.pen.trans = newVal>=5?0:8;
	})
}])




